/*
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Authors:
 *  	Albert Marieges Garcia
 *  	Álex Pardo Fernández
 *  	Enric Mayas Márquez
 *  	Juan Carlos Calvo Tejedor
 *  	Marc Gironés Dezsényi
 *  
 *  Contact:
 *  	bcn.tours.android@gmail.com
 *  Webpage:
 *  	bcntours.co.cc
 *
 */
package com.BCNTours.Vista;

import java.util.ArrayList;
import java.util.Vector;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.net.ParseException;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.EditText;
import android.widget.ListView;

import com.BCNTours.R;
import com.BCNTours.Info.Vista.InfoActivity;
import com.BCNTours.Model.ApplicationData;
import com.BCNTours.Model.Attraction;
import com.BCNTours.Model.BasicInfoPoi;
import com.BCNTours.Model.InformationPoint;
import com.BCNTours.Model.Monument;
import com.BCNTours.Model.Museum;

/**
 * Displays a list of POIs resulting of the search of a particular string between all of the POIs.
 *
 */
public class Search extends ListActivity {
	
	Bundle extras;
	private int tipo;
	private ApplicationData applicationData;
	private Vector<RowData> data;
	private RowData rd;
	private CustomAdapter adapter;
	
	private EditText searchText;
	
	private ArrayList<BasicInfoPoi> basicList;
	private ArrayList<Attraction> attractions;
	private ArrayList<Monument> monuments;
	private ArrayList<Museum> museums;
	private ArrayList<InformationPoint> informationPoints;

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search);
	
        extras = getIntent().getExtras();
        applicationData = (ApplicationData) extras.getSerializable("com.ShowMe.Model.PoiList");
        tipo = extras.getInt("tipo");
        
        searchText = (EditText)findViewById(R.id.searchText);
        
        basicList = applicationData.getBasicList();
    	attractions = applicationData.getAttractions();
    	monuments = applicationData.getMonuments();
    	museums = applicationData.getMuseums();
    	informationPoints = applicationData.getInformationPoints();
        
        completeList();

        searchText.setOnKeyListener(new OnKeyListener() {
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				//if (event.getAction() == KeyEvent.ACTION_DOWN)
		        //    if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER){
		            	search();
		        //    }
		          return false;
			}
        });
        
        
        
        
        
	}
	
	private void search() {
		
		switch(tipo){
		//Attractions
		case 0:
			attractions = applicationData.searchInAttractions(searchText.getText().toString());
			completeList();
    		break;
		
    	//Monuments
    	case 1:
    		monuments = applicationData.searchInMonuments(searchText.getText().toString());
    		completeList();
    		break;
    		
    	//Museums
    	case 2:    
    		museums = applicationData.searchInMuseums(searchText.getText().toString());
    		completeList();
    		break;
    		
    	//Information points
    	case 3:  
    		informationPoints = applicationData.searchInInformationPoints(searchText.getText().toString());
    		completeList();
    		break;
	
    	//All
    	case 4:    	
    		basicList = applicationData.searchInAll(searchText.getText().toString());
    		completeList();
    		break;
    		
		}
		
	}
	
	
	/**
     * The action listener of the list
     */
    public void onListItemClick(ListView parent, View v, int positionPoi, long id) { 
    	
    	Intent intent = new Intent(getApplicationContext(), InfoActivity.class);
    	
    	Attraction att;
    	Monument mon;
    	Museum mus;
    	InformationPoint inf;
    	
    	intent.putExtra("unit", applicationData.getUnit());
    	
    	switch(tipo){
		//Attractions
		case 0:
			att = applicationData.getAttractions().get(positionPoi);
			intent.putExtra("com.ShowMe.Model.Attraction", att);
			intent.putExtra("tipo", tipo);
			break;
		//Monuments
		case 1:
			mon = applicationData.getMonuments().get(positionPoi);
			intent.putExtra("com.ShowMe.Model.Monument", mon);
			intent.putExtra("tipo", tipo);
			break;
		//Museums
		case 2:
			mus = applicationData.getMuseums().get(positionPoi);
			intent.putExtra("com.ShowMe.Model.Museum", mus);
			intent.putExtra("tipo", tipo);
			break;
    	//Information points
		case 3:
			inf = applicationData.getInformationPoints().get(positionPoi);
			intent.putExtra("com.ShowMe.Model.InformationPoint", inf);
			intent.putExtra("tipo", tipo);
			break;
    	//All
		case 4:
	    	BasicInfoPoi b = applicationData.getBasicList().get(positionPoi);
	    	if(b.getPoi() instanceof Attraction){
	    		att = (Attraction) b.getPoi();
				intent.putExtra("com.ShowMe.Model.Attraction", att);
				intent.putExtra("tipo", 0);
	    	} else if(b.getPoi() instanceof Monument){
	    		mon = (Monument) b.getPoi();
				intent.putExtra("com.ShowMe.Model.Monument", mon);
				intent.putExtra("tipo", 1);
	    	} else if(b.getPoi() instanceof Museum){
	    		mus = (Museum) b.getPoi();
				intent.putExtra("com.ShowMe.Model.Museum", mus);
				intent.putExtra("tipo", 2);
	    	} else if(b.getPoi() instanceof InformationPoint){
	    		inf = (InformationPoint) b.getPoi();
				intent.putExtra("com.ShowMe.Model.InformationPoint", inf);
				intent.putExtra("tipo", 3);
	    	}
		}
    	
    	startActivity(intent);
				
    	setListAdapter(adapter);
        getListView().setTextFilterEnabled(true);
    }
	
	private void completeList () {
		
		LayoutInflater mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        
        data = new Vector<RowData>();
        
        switch(tipo){
			//Attractions
			case 0:
				this.setTitle(getString(R.string.msgAttracions));
				for(int i = 0; i < attractions.size(); ++i){
					try {
						rd = new RowData(i, attractions.get(i).getName(), attractions.get(i).getImage());
					} catch (ParseException e) {
						e.printStackTrace();
					}
					data.add(rd);
		    	}
	    		break;
			
	    	//Monuments
	    	case 1:
	    		this.setTitle(getString(R.string.msgMonuments));
	    		for(int i = 0; i < monuments.size(); ++i){
					try {
						rd = new RowData(i, monuments.get(i).getName(), monuments.get(i).getImage());
					} catch (ParseException e) {
						e.printStackTrace();
					}
					data.add(rd);
		    	}
	    		break;
	    		
	    	//Museums
	    	case 2:    
	    		this.setTitle(getString(R.string.msgMuseums));
	    		for(int i = 0; i < museums.size(); ++i){
					try {
						rd = new RowData(i, museums.get(i).getName(), museums.get(i).getImage());
					} catch (ParseException e) {
						e.printStackTrace();
					}
					data.add(rd);
		    	}
	    		break;
	    		
	    	//Information points
	    	case 3:  
	    		this.setTitle(getString(R.string.msgInfoPoint));
	    		for(int i = 0; i < informationPoints.size(); ++i){
					try {
						rd = new RowData(i, informationPoints.get(i).getName(), informationPoints.get(i).getImage());
					} catch (ParseException e) {
						e.printStackTrace();
					}
					data.add(rd);
		    	}
	    		break;
    	
	    	//All
	    	case 4:    	
	    		this.setTitle(getString(R.string.msgAll));
	    		for(int i = 0; i < basicList.size(); ++i){
					try {
						rd = new RowData(i, basicList.get(i).getName(), basicList.get(i).getImage());
					} catch (ParseException e) {
						e.printStackTrace();
					}
					data.add(rd);
		    	}
	    		break;
	    		
        }
        
        adapter = new CustomAdapter(this, mInflater ,R.layout.favorites_list, R.id.title, data);
        setListAdapter(adapter);
        getListView().setTextFilterEnabled(true);
	}
	
}

/*package com.BCNTours.Vista;

import com.BCNTours.R;

import android.app.ListActivity;
import android.os.Bundle;

public class Search extends ListActivity {
	
	/*Attributions*/
	/*private ListView myListView;
	private EditText searchEditText;
	private ArrayList<String> searchItems;
	private ArrayAdapter<String> aa;
	private ApplicationData applicationData;
	private ArrayList<Attraction> searchResult;
	private int type;
	Bundle extras;
	*/
	
	
	
	
	/*private Vector<RowData> data;
	private RowData rd;
	private CustomAdapter adapter;
	*/
	
	
	//private ListView searchList;
	//private EditText searchText;
	
	
	//Bundle extras;
	//private ApplicationData applicationData;
	//private Vector<RowData> data;
	//private RowData rd;
	//private CustomAdapter adapter;
	
	
	/** Called when the activity is first created. */
	/*public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		
		
		
		// Inflate your view
		setContentView(R.layout.search);
		
		
		//searchList = (ListView)findViewById(R.id.searchList);
		
		//myListView.setOnItemClickListener(myOnClickListener);
		//searchText = (EditText)findViewById(R.id.searchText);
		
		
		/*extras = getIntent().getExtras();
		applicationData = (ApplicationData) extras.getSerializable("com.ShowMe.Model.PoiList");
		
		
		
		LayoutInflater mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        
        data = new Vector<RowData>();
        
        for(int i = 0; i < applicationData.getAttractions().size(); ++i){
        	try {
        		rd = new RowData(i, applicationData.getAttractions().get(i).getName(), applicationData.getAttractions().get(i).getImage());
			} catch (ParseException e) {
				e.printStackTrace();
			}
			data.add(rd);
        }
	    
		
        adapter = new CustomAdapter(this, mInflater ,R.layout.favorites_list, R.id.title, data);
        setListAdapter(adapter);
        getListView().setTextFilterEnabled(true);
        
        
        */
		
		/*
		
		extras = getIntent().getExtras();
        //image = extras.getInt("listImage");
        applicationData = (ApplicationData) extras.getSerializable("com.ShowMe.Model.PoiList");
        //tipo = extras.getInt("tipo");
		
        
        
		//Animation of the screen
		LayoutInflater mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
		
		data = new Vector<RowData>();
		
		  
		// Get references to UI widgets
		//myListView = (ListView)findViewById(R.id.myListView);
		
		//myListView.setOnItemClickListener(myOnClickListener);
		//searchEditText = (EditText)findViewById(R.id.txtSearch);
		
		//searchItems = new ArrayList<String>();
		//int resID = R.layout.search_list_item;
		//aa = new ArrayAdapter<String>(this, resID, searchItems);
		//myListView.setAdapter(aa);
		
		
		
		/*data = new Vector<RowData>();
	        
		for(int i = 0; i < appData.getAttractions().size(); ++i){
			try {
				rd = new RowData(i, appData.getAttractions().get(i).getName(), appData.getAttractions().get(i).getImage());
			} catch (ParseException e) {
				e.printStackTrace();
			}
			data.add(rd);
		}*/
		
		
		
		
		
		
		
		
		/*
		//Action listener of the searchEditText
	    searchEditText.setOnKeyListener(new OnKeyListener() {
	        public boolean onKey(View v, int keyCode, KeyEvent event) {
	          if (event.getAction() == KeyEvent.ACTION_DOWN)
	            if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER){
	            	searchElement();
	            }
	          return false;
	        }
	      });
	    
	    //On change listener of the searchEditText
		searchEditText.addTextChangedListener(new TextWatcher() {
			
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
			}
			
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			
			public void afterTextChanged(Editable s) {
				searchElement();
				// TODO Auto-generated method stub
			}
		}); 
		*/
//	}
	
	/*private OnItemClickListener myOnClickListener = new OnItemClickListener() {

		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// TODO Auto-generated method stub
			Intent intent = new Intent(getApplicationContext(), InfoActivity.class);
	    	
			
	    	/*switch(searchResult.get(position).getType()){
				
				
				
				//Monuments
				case 1:
					Monument mon = (Monument) searchResult.get(position);
					intent.putExtra("com.ShowMe.Model.Monument", mon);
					intent.putExtra("tipo", 1);
					break;
					//Attractions
				case 2:
					Attraction att = (Attraction) searchResult.get(position);
					intent.putExtra("com.ShowMe.Model.Attraction", att);
					intent.putExtra("tipo", 0);
					break;
					
				//Museums
				case 3:
					Museum mus = (Museum) searchResult.get(position);
					intent.putExtra("com.ShowMe.Model.Museum", mus);
					intent.putExtra("tipo", 2);
					break;
		    	//Information points
				case 4:
					InformationPoint inf = (InformationPoint) searchResult.get(position);
					intent.putExtra("com.ShowMe.Model.InformationPoint", inf);
					intent.putExtra("tipo", 3);
					break;
				default:
					break;
	    	}*/
	    	
	    	/*startActivity(intent);
			
		}
	};*/
	
    /**
     * Create the menu to the searchLayout
     */
    /*@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menusearch, menu);
		return true;
    }*/
    
	/**
	 * The searcher from the database and updates the list
	 */
	/*private void searchElement(){
		String str;
		
		/*Remove all element from the list*/
    	//searchItems.removeAll(searchItems);
    	
    	/*Search the text from the database*/
//    	str = "(?i).*" + searchEditText.getText().toString() + ".*";
    	//str = searchEditText.getText().toString();
    	/*
    	for(int i = 0; i < listObject.length; i++){
    		if(listObject[i].toString().matches(str)){
    			searchItems.add(0, listObject[i]);
    		}
    	}*/
    	/*if(!str.equals("")){
    		searchResult = applicationData.searchInAll(str);
	    	/*if(type == 4){
	    		searchResult = appData.searchInAll(str);
	    	} else{
	    		searchResult = appData.searchCategory(str, type);
	    	}
	    	
	    	for(int i = 0; i < searchResult.size(); i++){
	    		searchItems.add(searchResult.get(i).getName());
	    	}
    	}
    	
    	/*Updates the ArrayAdapter*/
		/*aa.notifyDataSetChanged();
	}*/
	
	
    /**
     * BtnActionListener of search
     */
	/*public void btnSearchActionPerformed(View view){
		/*switch(view.getId()){
			case R.id.btnSearch:
				searchElement();
				break;
		}*/
	//}
//}